package com.cyt.gen.utils;

import cn.org.rapid_framework.generator.GeneratorFacade;
import cn.org.rapid_framework.generator.GeneratorProperties;
import com.cyt.gen.model.vo.GeneratorVo;

/**
 * @author rong yuan
 * @date 2019/7/4 13:32
 */
public class GeneratorBaseUtil extends GeneratorUtil {

    private String outRootXml;
    private String templateRootXml;

    public GeneratorBaseUtil(GeneratorVo generatorVo) {
        super(generatorVo);
        this.outRootJava = projectPath + "cyt-" + packageName + "/src/main/java/com/cyt/" + moduleName;
        this.templateRootJava = projectPath + "cyt-gen/src/main/resources/template/base/java/";
        this.outRootController = projectPath + "cyt-web/src/main/java/com/cyt/web/controller/" + moduleName;
        this.templateRootController = projectPath + "cyt-gen/src/main/resources/template/base/web/";
        this.outRootXml = projectPath + "cyt-" + packageName + "/src/main/resources/mapping/" + moduleName;
        this.templateRootXml = projectPath + "cyt-gen/src/main/resources/template/base/xml/";
    }

    @Override
    public void generatorCode() throws Exception {
        //删除已生成或已存在代码
        deleteGeneratorCode();
        //初始化配置属性
        initGeneratorProperties();
        //生成代码
        startGeneratorCode();
    }

    @Override
    public void startGeneratorCode() throws Exception {
        super.startGeneratorCode();
        //生成xml代码
        GeneratorProperties.setProperty("outRoot", outRootXml);
        GeneratorFacade generatorFacade = new GeneratorFacade();
        generatorFacade.getGenerator().setTemplateRootDir(templateRootXml);
        generatorFacade.generateByTable(tableName);
    }
}